package com.e3.basdoc.corp.server;

import java.util.ArrayList;
import java.util.List;

import com.e3.basdoc.corp.client.CorpHelper;
import com.e3.basdoc.corp.client.CorpRPCService;
import com.e3.basdoc.corp.model.CorpModel;
import com.e3.basdoc.corp.model.CorpQC;
import com.e3.basdoc.region.model.RegionModel;
import com.e3.basdoc.region.server.RegionServiceClient;
import com.e3.core.base.ServiceException;
import com.e3.core.base.SessionContext;
import com.e3.web.core.client.WebException;
import com.e3.web.core.server.GwtRPCAction;
import com.e3.web.core.server.GwtRPCExecutor;
import com.e3.web.core.server.GwtRPCVoidExecutor;

public class CorpRPCServiceImpl extends GwtRPCAction implements CorpRPCService {

	public CorpModel newCorp(final CorpModel corpModel) throws WebException {
		return new GwtRPCExecutor<CorpModel>() {

			@Override
			public CorpModel execute() throws ServiceException, Exception {
				return CorpServiceClient.getCorpService().newCorp(corpModel);
			}

		}.doExecute();
	}

	public List<CorpModel> getCorpsByCondition(final CorpQC corpQC) throws WebException {
		return new GwtRPCExecutor<List<CorpModel>>() {

			@Override
			public List<CorpModel> execute() throws ServiceException, Exception {
				List<CorpModel> corps = CorpServiceClient.getCorpQueryService().getCorpByCondition(corpQC);
				if (corps == null || corps.size() <= 0) {
					return corps;
				}
				fillRegion(corps);
				fillParentCorp(corps);
				return corps;
			}

		}.doExecute();
	}

	public CorpModel modifyCorp(final CorpModel corpModel) throws WebException {
		return new GwtRPCExecutor<CorpModel>() {

			@Override
			public CorpModel execute() throws ServiceException, Exception {
				return CorpServiceClient.getCorpService().modifyCorp(corpModel);
			}

		}.doExecute();
	}

	public void deleteCorps(final List<CorpModel> corpModels) throws WebException {
		new GwtRPCVoidExecutor() {

			@Override
			public void execute() throws ServiceException, Exception {
				CorpServiceClient.getCorpService().deleteCorps(corpModels);
			}

		}.doExecute();
	}

	public List<CorpModel> getCorpChildren(final CorpModel corpModel) throws WebException {
		return new GwtRPCExecutor<List<CorpModel>>() {

			@Override
			public List<CorpModel> execute() throws ServiceException, Exception {
				List<CorpModel> models = new ArrayList<CorpModel>();
				if (corpModel == null) {
					CorpModel model = CorpHelper.INSTANCE.createRootModel();
					models.add(model);
					return models;
				}

				CorpModel qModel = corpModel;
				if (CorpHelper.INSTANCE.isRootModel(qModel)) {
					qModel = null;
				}
				
				List<CorpModel> result = null;
				if(SessionContext.isLocalCorp()||SessionContext.isSuperAdimin()){
					result = CorpServiceClient.getCorpQueryService().getCorpChildren(qModel);
				}
				else{
					result = CorpServiceClient.getCorpQueryService().getCorpModelByIds(SessionContext.getCorpID());
				}
				
				CorpHelper.INSTANCE.setCorpRelations(corpModel, result);
				fillRegion(result);
				return result;
			}

		}.doExecute();
	}

	public CorpModel getCorpById(final String id) throws WebException {
		return new GwtRPCExecutor<CorpModel>() {

			@Override
			public CorpModel execute() throws ServiceException, Exception {
				return CorpServiceClient.getCorpQueryService().getCorpModelById(Long.parseLong(id));
			}

		}.doExecute();
	}

	private void fillRegion(List<CorpModel> corps) throws Exception {
		if(corps==null){
			return;
		}
		// 填充Corp的所在地区
		List<Long> regionIds = new ArrayList<Long>(corps.size());
		for (CorpModel corp : corps) {
			Long regionId = corp.getRegionId();
			if (!regionIds.contains(regionId)) {
				regionIds.add(regionId);
			}
		}
		if (!regionIds.isEmpty()) {
			Long[] ids = new Long[regionIds.size()];
			regionIds.toArray(ids);
			List<RegionModel> regions = RegionServiceClient.getRegionQueryService().getRegionModelByIds(ids);
			for (CorpModel corp : corps) {
				Long regionId = corp.getRegionId();
				for (RegionModel region : regions) {
					Long id = region.getId();
					if (regionId.equals(id)) {
						corp.setRegion(region);
						break;
					}
				}
			}
		}
	}

	private void fillParentCorp(List<CorpModel> corps) throws Exception {
		// 填充Corp的上级Corp
		List<Long> parentIds = new ArrayList<Long>(corps.size());
		for (CorpModel corp : corps) {
			if (!CorpHelper.INSTANCE.isTopModel(corp)) {
				Long parentId = corp.getParentId();
				if (!parentIds.contains(parentId)) {
					parentIds.add(parentId);
				}
			}
		}
		if (!parentIds.isEmpty()) {
			Long[] ids = new Long[parentIds.size()];
			parentIds.toArray(ids);
			List<CorpModel> parentCorps = CorpServiceClient.getCorpQueryService().getCorpModelByIds(ids);
			for (CorpModel corp : corps) {
				Long pID = corp.getParentId();
				for (CorpModel corpModel : parentCorps) {
					Long id = corpModel.getId();
					if (pID.equals(id)) {
						corp.setParentCorp(corpModel);
						break;
					}
				}
			}
		}
	}

}
